<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Motan,springboot," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="Motanotan是一套基于java开发的RPC框架，除了常规的点对点调用外，Motan还提供服务治理功能，包括服务节点的自动发现、摘除、高可用和负载均衡等。Motan具有良好的扩展性，主要模块都提供了多种不同的实现，例如支持多种注册中心，支持多种rpc协议等。 springboot集成Motan 1、pom.xml文件中引入对应的jar (Maven)&amp;lt;dependency&amp;gt;">
<meta name="keywords" content="Motan,springboot">
<meta property="og:type" content="article">
<meta property="og:title" content="springboot整合Motan">
<meta property="og:url" content="https://gitee.com/shuhao/2017/12/08/springboot整合Motan/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="Motanotan是一套基于java开发的RPC框架，除了常规的点对点调用外，Motan还提供服务治理功能，包括服务节点的自动发现、摘除、高可用和负载均衡等。Motan具有良好的扩展性，主要模块都提供了多种不同的实现，例如支持多种注册中心，支持多种rpc协议等。 springboot集成Motan 1、pom.xml文件中引入对应的jar (Maven)&amp;lt;dependency&amp;gt;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-08T12:40:52.925Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="springboot整合Motan">
<meta name="twitter:description" content="Motanotan是一套基于java开发的RPC框架，除了常规的点对点调用外，Motan还提供服务治理功能，包括服务节点的自动发现、摘除、高可用和负载均衡等。Motan具有良好的扩展性，主要模块都提供了多种不同的实现，例如支持多种注册中心，支持多种rpc协议等。 springboot集成Motan 1、pom.xml文件中引入对应的jar (Maven)&amp;lt;dependency&amp;gt;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2017/12/08/springboot整合Motan/"/>





  <title> springboot整合Motan | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2017/12/08/springboot整合Motan/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                springboot整合Motan
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-08T15:29:03+08:00">
                2017-12-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Motan/" itemprop="url" rel="index">
                    <span itemprop="name">Motan</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="Motan"><a href="#Motan" class="headerlink" title="Motan"></a>Motan</h2><p>otan是一套基于java开发的RPC框架，除了常规的点对点调用外，Motan还提供服务治理功能，包括服务节点的自动发现、摘除、高可用和负载均衡等。Motan具有良好的扩展性，主要模块都提供了多种不同的实现，例如支持多种注册中心，支持多种rpc协议等。</p>
<p>springboot集成Motan</p>
<h3 id="1、pom-xml文件中引入对应的jar-Maven"><a href="#1、pom-xml文件中引入对应的jar-Maven" class="headerlink" title="1、pom.xml文件中引入对应的jar (Maven)"></a>1、pom.xml文件中引入对应的jar (Maven)</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.weibo&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;motan-core&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.1&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.weibo&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;motan-springsupport&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.1&lt;/version&gt;</span><br><span class="line">    &lt;exclusions&gt;</span><br><span class="line">        &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;</span><br><span class="line">        &lt;/exclusion&gt;</span><br><span class="line">    &lt;/exclusions&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.weibo&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;motan-transport-netty&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.1&lt;/version&gt;</span><br><span class="line">    &lt;exclusions&gt;</span><br><span class="line">        &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;</span><br><span class="line">        &lt;/exclusion&gt;</span><br><span class="line">    &lt;/exclusions&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.weibo&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;motan-registry-zookeeper&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.1&lt;/version&gt;</span><br><span class="line">    &lt;exclusions&gt;</span><br><span class="line">        &lt;exclusion&gt;</span><br><span class="line">            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;</span><br><span class="line">            &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;</span><br><span class="line">        &lt;/exclusion&gt;</span><br><span class="line">    &lt;/exclusions&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>
<p>排除Motan中引入的日志</p>
<h3 id="2、在application-properties文件中添加server配置项"><a href="#2、在application-properties文件中添加server配置项" class="headerlink" title="2、在application.properties文件中添加server配置项"></a>2、在application.properties文件中添加server配置项</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#Motan解析的包名</span><br><span class="line">motan.annotation=com.totoro.motan.service</span><br><span class="line">#Motan protocol</span><br><span class="line">motan.protocol.id=motan</span><br><span class="line">motan.protocol.name=motan</span><br><span class="line">#负责均衡策略 ActiveWeight, Random, RoundRobin,LocalFirst, Consistent, ConfigurableWeight</span><br><span class="line">motan.protocol.loadbalance=Random</span><br><span class="line">#容错策略 Failover, Failfast</span><br><span class="line">motan.protocol.haStrategy=Failover</span><br><span class="line">#服务端连接池工作线程数</span><br><span class="line">motan.protocol.maxWorkerThread=10</span><br><span class="line">motan.protocol.minWorkerThread=2</span><br><span class="line">#客户端对每隔服务建立的连接数</span><br><span class="line">motan.protocol.maxClientConnection=10</span><br><span class="line">motan.protocol.minClientConnection=2</span><br><span class="line"></span><br><span class="line">#Motan registry</span><br><span class="line">motan.registry.name=registry_zookeeper</span><br><span class="line">#注册中心协议 zookeeper, consul</span><br><span class="line">motan.registry.regProtocol=zookeeper</span><br><span class="line">#注册中心地址</span><br><span class="line">motan.registry.address=127.0.0.1:2181</span><br><span class="line">#注册中心端口</span><br><span class="line">#motan.registry.port=2181</span><br><span class="line"></span><br><span class="line">#Motan service</span><br><span class="line">#服务的暴露方式 protocolId:port</span><br><span class="line">motan.service.export=motan:9991</span><br><span class="line">#服务分组</span><br><span class="line">motan.service.group=local_motan</span><br><span class="line">#服务模块</span><br><span class="line">motan.service.module=demo</span><br><span class="line">#server使用的注册中心</span><br><span class="line">motan.service.registry=registry_zookeeper</span><br></pre></td></tr></table></figure>
<h3 id="3、添加server的配置类"><a href="#3、添加server的配置类" class="headerlink" title="3、添加server的配置类"></a>3、添加server的配置类</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.totoro.motan.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.ProtocolConfig;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.RegistryConfig;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.AnnotationBean;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.ProtocolConfigBean;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.RegistryConfigBean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.CommandLineRunner;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.annotation.Order;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.StringUtils;</span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MotanConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Motan扫描包路径配置</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> scanPackage</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AnnotationBean <span class="title">motanAnnotationBean</span><span class="params">(@Value(<span class="string">"$&#123;motan.annotation&#125;"</span>)</span> String scanPackage)</span>&#123;</span><br><span class="line">        AnnotationBean annotationBean = <span class="keyword">new</span> AnnotationBean();</span><br><span class="line">        <span class="keyword">if</span>(!StringUtils.isEmpty(scanPackage))</span><br><span class="line">            annotationBean.setPackage(scanPackage);</span><br><span class="line">        <span class="keyword">return</span> annotationBean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Motan协议配置</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span>(name = <span class="string">"motan"</span>)</span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.protocol"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ProtocolConfig <span class="title">protocolConfig</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ProtocolConfig protocolConfig = <span class="keyword">new</span> ProtocolConfig();</span><br><span class="line">        <span class="keyword">return</span> protocolConfig;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span>(name = <span class="string">"registry_zookeeper"</span>)</span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.registry"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> RegistryConfig <span class="title">registryConfig</span><span class="params">()</span></span>&#123;</span><br><span class="line">        RegistryConfig registryConfig = <span class="keyword">new</span> RegistryConfig();</span><br><span class="line">        <span class="keyword">return</span> registryConfig;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.totoro.motan.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.CommandLineRunner;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.annotation.Order;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MotanServiceConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.service"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> BasicServiceConfigBean <span class="title">motanServerConfig</span><span class="params">()</span></span>&#123;</span><br><span class="line">        BasicServiceConfigBean basicServiceConfigBean = <span class="keyword">new</span> BasicServiceConfigBean();</span><br><span class="line">        <span class="keyword">return</span> basicServiceConfigBean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4、服务接口的实现"><a href="#4、服务接口的实现" class="headerlink" title="4、服务接口的实现"></a>4、服务接口的实现</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.totoro.motan.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.totoro.motan.service.IService;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.annotation.MotanService;</span><br><span class="line"></span><br><span class="line"><span class="meta">@MotanService</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServiceImpl</span> <span class="keyword">implements</span> <span class="title">IService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">say</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"我要你在我身旁!"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在这里接口的实现类使用的注解是@MotanService 而不是Spring的注解@Service</p>
<h3 id="5、在服务启动各类中添加Motan对外提供服务的开关"><a href="#5、在服务启动各类中添加Motan对外提供服务的开关" class="headerlink" title="5、在服务启动各类中添加Motan对外提供服务的开关"></a>5、在服务启动各类中添加Motan对外提供服务的开关</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.totoro.motan;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.common.MotanConstants;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.util.MotanSwitcherUtil;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.SpringApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(Application.class, args);</span><br><span class="line">        MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, <span class="keyword">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>motan是通过这个开关控制是否对外提供rpc服务的。<br>一般正式环境使用时是需要验证服务正常后才正式对外提供服务的，防止有问题的服务注册到线上。另外在停机的时候，可以先通过开关暂停服务，等没有请求后在进行shutdown。</p>
<h3 id="6、在application-properties文件中添加client配置"><a href="#6、在application-properties文件中添加client配置" class="headerlink" title="6、在application.properties文件中添加client配置"></a>6、在application.properties文件中添加client配置</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#Motan Registry</span><br><span class="line">#注册中心协议 zookeeper, consul</span><br><span class="line">motan.registry.regProtocol=zookeeper</span><br><span class="line">#注册中心地址</span><br><span class="line">motan.registry.address=127.0.0.1:2181</span><br><span class="line">#注册中心连接超时时间(毫秒)</span><br><span class="line">motan.registry.requestTimeout=1000</span><br><span class="line">#注册中心请求超时时间(毫秒)</span><br><span class="line">motan.registry.connectTimeout=3000 </span><br><span class="line"></span><br><span class="line">#Motan Referer</span><br><span class="line">motan.referer.group=local_motan</span><br><span class="line">motan.referer.module=demo</span><br><span class="line">motan.referer.protocol=motan</span><br><span class="line">motan.referer.registry=registryConfig</span><br></pre></td></tr></table></figure>
<h3 id="7、添加client配置类"><a href="#7、添加client配置类" class="headerlink" title="7、添加client配置类"></a>7、添加client配置类</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.totoro.motan.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.ProtocolConfig;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.RegistryConfig;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.AnnotationBean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MotanConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.annotation"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> AnnotationBean <span class="title">annotationBean</span><span class="params">()</span></span>&#123;</span><br><span class="line">        AnnotationBean annotationBean = <span class="keyword">new</span> AnnotationBean();</span><br><span class="line">        <span class="keyword">return</span> annotationBean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span>(name = <span class="string">"motan"</span>)</span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.protocol"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ProtocolConfig <span class="title">protocolConfig</span><span class="params">()</span></span>&#123;</span><br><span class="line">        ProtocolConfig protocolConfig = <span class="keyword">new</span> ProtocolConfig();</span><br><span class="line">        <span class="keyword">return</span> protocolConfig;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span>()</span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.registry"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> RegistryConfig <span class="title">registryConfig</span><span class="params">()</span></span>&#123;</span><br><span class="line">        RegistryConfig registryConfig = <span class="keyword">new</span> RegistryConfig();</span><br><span class="line">        <span class="keyword">return</span> registryConfig;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> com.totoro.motan.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.BasicRefererConfigBean;</span><br><span class="line"><span class="keyword">import</span> com.weibo.api.motan.config.springsupport.annotation.MotanReferer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.context.properties.ConfigurationProperties;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MotanClientConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@ConfigurationProperties</span>(prefix = <span class="string">"motan.referer"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> BasicRefererConfigBean <span class="title">basicRefererConfigBean</span><span class="params">()</span></span>&#123;</span><br><span class="line">        BasicRefererConfigBean basicRefererConfigBean = <span class="keyword">new</span> BasicRefererConfigBean();</span><br><span class="line">        <span class="keyword">return</span> basicRefererConfigBean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="8、调用服务接口"><a href="#8、调用服务接口" class="headerlink" title="8、调用服务接口"></a>8、调用服务接口</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@MotanReferer</span></span><br><span class="line">  IService iService;</span><br></pre></td></tr></table></figure>
<h4 id="在整合过程中遇到的问题："><a href="#在整合过程中遇到的问题：" class="headerlink" title="在整合过程中遇到的问题："></a>在整合过程中遇到的问题：</h4><p>1、Motan参数配置motan.service.export=motan:9991 为motan的protocolId:prot。port为服务暴露供调用的端口</p>
<p>2、在配置Motan的协议时，开始至配置了motan.protocol.name,没有配置motan.protocol.id然后在实例化BasicRefererConfigBean这个对象时报错，<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Consider defining a bean named &apos;motan&apos; in your configuration</span><br></pre></td></tr></table></figure></p>
<p>找不到motan这个ProtocolConfig对象，开始以为是实例化顺序的问题，使用Spring的@Order标签，改变实例化的顺序，没有效果；然后用了CommandLineRunner这个接口，想改变实例化的顺序，也失败了（只改变了接口实现方法run执行的顺序）。最后跟进代码（Intellij这点非常方便）才发现在BasicRefererConfigBean中通过id这个参数来回去ProtocolConfig这个对象，所以，配置motan.protocol.id=motan解决了这个问题。</p>
<p>3、在配置motan的注册中心motan.service.registry时，值要为Springboot实例化的RegistryConfig 对象的名称。这也是为什么在RegistryConfig配置项时使用@Bean(name = “registry_zookeeper”)。注意保持这两个地方一致。</p>
<p>4、与第三项类似的为ProtocolConfig配置项的motan.protocol.name 与ProtocolConfig配置项时使用@Bean(name = “motan”)保持一致</p>
<p><a href="https://gitee.com/shuhao/code/tree/master/motan/spring-boot-motan-demo">demo</a> </p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Motan/" rel="tag"># Motan</a>
          
            <a href="/blog/tags/springboot/" rel="tag"># springboot</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/07/Mongo查询分析以及索引规则/" rel="next" title="Mongo查询分析以及索引规则一">
                <i class="fa fa-chevron-left"></i> Mongo查询分析以及索引规则一
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/13/Elasticsearch安装/" rel="prev" title="Elasticsearch安装">
                Elasticsearch安装 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Motan"><span class="nav-number">1.</span> <span class="nav-text">Motan</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、pom-xml文件中引入对应的jar-Maven"><span class="nav-number">1.1.</span> <span class="nav-text">1、pom.xml文件中引入对应的jar (Maven)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、在application-properties文件中添加server配置项"><span class="nav-number">1.2.</span> <span class="nav-text">2、在application.properties文件中添加server配置项</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、添加server的配置类"><span class="nav-number">1.3.</span> <span class="nav-text">3、添加server的配置类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、服务接口的实现"><span class="nav-number">1.4.</span> <span class="nav-text">4、服务接口的实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5、在服务启动各类中添加Motan对外提供服务的开关"><span class="nav-number">1.5.</span> <span class="nav-text">5、在服务启动各类中添加Motan对外提供服务的开关</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6、在application-properties文件中添加client配置"><span class="nav-number">1.6.</span> <span class="nav-text">6、在application.properties文件中添加client配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7、添加client配置类"><span class="nav-number">1.7.</span> <span class="nav-text">7、添加client配置类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8、调用服务接口"><span class="nav-number">1.8.</span> <span class="nav-text">8、调用服务接口</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#在整合过程中遇到的问题："><span class="nav-number">1.8.1.</span> <span class="nav-text">在整合过程中遇到的问题：</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
